﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PeertoPeerObjectShare.PacketHandler
{
    public class PacketResolver
    {


        public static PackageHandler get_Package(string jsonstring)
        {
            PackageHandler handle  = null;
            try
            {

                JObject o = JObject.Parse(jsonstring);

               // string type = o.GetType().ToString();
                string type = (string)o["packtype"];

                switch (type.ToUpper())
                {


                    case ("CHAT"):
                        return new ChatPackage((string)o["fromaddr"], (string)o["destaddr"], (string)o["message"]);

                    case ("COMMENT"):
                        return new CommentPackage((string)o["fromaddr"], (string)o["destaddr"], (string)o["message"]);

                    case ("LINK"):
                        return new LinkPackage((string)o["fromaddr"], (string)o["destaddr"], (string)o["message"]);

                    case ("WALLPOST"):
                        return new WallpostPackage((string)o["fromaddr"], (string)o["destaddr"], (string)o["message"]);

                    default:
                        return new PackageHandler();

                }
            }
            catch (JsonException exception)
            {
                Console.WriteLine("CANNOT GET PACKAGE JSON", exception.Message);

            }

            return handle;

        }
        

        public static PackageContainer get_Container(string jsonstring)
        {
            PackageContainer container = null;

            try
            {

                JObject o = JObject.Parse(jsonstring);
                container = new PackageContainer((string)o["fromaddr"], (string)o["destaddr"],(string)o["message"]);
                return container;

            }
            catch(JsonException exception){
                Console.WriteLine("CANNOT GET CONTAINER JSON", exception.Message);

            }

            return container;

        }

        public static string get_From(string jsonstring)
        {

            try
            {
                JObject o = JObject.Parse(jsonstring);
                return (string)o["fromaddr"];
            }

            catch (JsonException exception)
            {
                Console.WriteLine(exception.Message, "Error Parsing FROM Address JSON");
            }
            return string.Empty;

        }

        public static string get_Message(string jsonstring)
        {
            try
            {
                JObject o = JObject.Parse(jsonstring);
                return (string)o["message"];
            }

            catch (JsonException exception)
            {
                Console.WriteLine(exception.Message, "Error Parsing MESSAGE Address JSON");
            }
            return string.Empty;


        }

        public static string get_To(string jsonstring)
        {
            try
            {
                JObject o = JObject.Parse(jsonstring);
                return (string)o["destaddr"];
            }

            catch (JsonException exception)
            {
                Console.WriteLine(exception.Message, "Error Parsing DEST Address JSON");
            }
            return string.Empty;

        }

    }
}
